package com.bbx.taxi.client.widget.pop;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;

import com.bbx.taxi.official.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class UploadPhotosPopView implements OnClickListener {
	private static Activity context;
	private LayoutInflater inflater = null;
	public PopupWindow mPopupWindow;
	private int crop = 180;
	public boolean sdCardExist = false; // 判断sd卡是否存在
	public static String sdcardTemp=Environment
			.getExternalStorageDirectory().getPath() + "/bangbangxing";
	public static File sdcardTempFile = new File(sdcardTemp,"tupian.tmp");
	public UploadPhotosPopView(Activity context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		sdCardExist = Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState());
		initPopView();
	}

	public void onShow(View v) {
		if(!mOnPhotosPoPViewStateListenerlist.isEmpty()){
			for(int i=0;i<mOnPhotosPoPViewStateListenerlist.size();i++){
				mOnPhotosPoPViewStateListenerlist.get(i).onPhotosPopViewState(true);
			}
		}
		mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
	}

	public void onDismiss() {
		mPopupWindow.dismiss();
	}

	public void initPopView() {
		View popview = inflater.inflate(R.layout.uploadphotospopview, null);
		mPopupWindow = new PopupWindow(popview, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setContentView(popview);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setFocusable(true);
		mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
		((Button) popview.findViewById(R.id.photograph))
				.setOnClickListener(this);
		((Button) popview.findViewById(R.id.picture)).setOnClickListener(this);
		((Button) popview.findViewById(R.id.close)).setOnClickListener(this);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {			
			@Override
			public void onDismiss() {
				if(!mOnPhotosPoPViewStateListenerlist.isEmpty()){
					for(int i=0;i<mOnPhotosPoPViewStateListenerlist.size();i++){
						mOnPhotosPoPViewStateListenerlist.get(i).onPhotosPopViewState(false);
					}
				}
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.photograph:
			  Uri uri = Uri.fromFile(sdcardTempFile);
		      Intent newIntent = new Intent("android.media.action.IMAGE_CAPTURE");
//		      newIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		      context.startActivityForResult(newIntent, 0);
			break;
		case R.id.picture:
			Intent intent2 = new Intent("android.intent.action.PICK");
			intent2.setDataAndType(
					MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
			context.startActivityForResult(intent2,1);
			break;
		case R.id.close:
			mPopupWindow.dismiss();
			break;
		}
	}

	public void cropImageUri(Uri uri,int requestCode) {
		 Intent intent = new Intent("com.android.camera.action.CROP");
	        intent.setDataAndType(uri, "image/*");
	        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
	        intent.putExtra("crop", "true");
	        // aspectX aspectY 是宽高的比例
	        intent.putExtra("aspectX", 1);
	        intent.putExtra("aspectY", 1);
	        // outputX outputY 是裁剪图片宽高
	        intent.putExtra("outputX", 150);
	        intent.putExtra("outputY", 150);
	        intent.putExtra(MediaStore.EXTRA_OUTPUT,
	                Uri.fromFile(new File(sdcardTempFile.getAbsoluteFile() + "tmp")));
		context.startActivityForResult(intent, requestCode);

	}

	public static byte[] readStream(InputStream inStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		return data;
	}

	public static Bitmap getPicFromBytes(byte[] bytes,
			BitmapFactory.Options opts) {
		if (bytes != null)
			if (opts != null)
				return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
						opts);
			else
				return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
		return null;
	}

	public static void cropImageUri(URI uri, int outputX, int outputY,
			int requestCode) {
		// TODO Auto-generated method stub

	}
	////
	/**
	 * 自定义拍照PopView开关状态监听
	 */
	 public  ArrayList<OnPhotosPoPViewStateListener>  mOnPhotosPoPViewStateListenerlist=new ArrayList<OnPhotosPoPViewStateListener>();
	/**
	 * 自定义拍照PopView开关状态监听
	 */
	public interface OnPhotosPoPViewStateListener {
		public void onPhotosPopViewState(boolean isOpen);
	}

	public  void setOnPhotosPoPViewStateListener(OnPhotosPoPViewStateListener hOnPhotosPoPViewStateListener) {
		mOnPhotosPoPViewStateListenerlist.add(hOnPhotosPoPViewStateListener);
	}
	public  void removeOnPhotosPoPViewStateListener(OnPhotosPoPViewStateListener hOnPhotosPoPViewStateListener){
		mOnPhotosPoPViewStateListenerlist.remove(hOnPhotosPoPViewStateListener);
	}
}
